#include "head.h"
void floor_main(){

    floor a;//定义结构体变量
    int b;//存放导航选择值
    int c=1;//存放循环值
    int d;//存放判添加楼是否存在
    int e=0;//防止重复导航提醒
    printf("1.增加新楼 2.查询楼栋 3.更改楼栋信息 4.删除楼栋 5.返回\n");
    b= hcq(b);
   while(c){
       if(e!=0){
           printf("1.增加新楼 2.查询楼栋 3.更改楼栋信息 4.删除楼栋 5.返回\n");
           b= hcq(b);
       }e++;
       switch (b) {
           case 1:
               printf("请输入要添加的楼栋名称,楼栋层数，建成日期:\n");
               while(1){
                   scanf("%s",a.floor_name);
                   scanf("%d",&a.tier_count);
                   scanf("%s",a.time);
                   d= insert_floor(a);
                   if(d==-1){
                       printf("该楼栋已存在，请重新输入:\n");
                       continue;
                   }else{
                       printf("添加楼栋成功\n");
                       break;
                   }
               }
               break;
           case 2:
               printf("\'all\'查询所有楼栋 或输入 \'楼栋名称\' 指定楼栋查询!\n");
               scanf("%s",a.floor_name);
               select_floor(a.floor_name);
               break;
           case 3:
               printf("请输入您要更改的楼栋名称:\n");
               while (1){
                   scanf("%s",a.floor_name);
                   d= judge_floor(a.floor_name);
                   if(d==-1){
                       continue;
                   } else{
                       update_floor(a.floor_name);
                       break;
                   }
               }
               break;
           case 4:
               printf("请输入您要删除的楼栋名称:\n");
               while(1){
                   scanf("%s",a.floor_name);
                   d=delete_floor(a.floor_name);
                   if(d == -1){
                       continue;
                   }else{
                       break;
                   }
               }
               break;
           case 5:
               c=0;
               break;
           default:
               printf("输入错误请重新输入:\n");
               break;
       }
   }
}

//添加楼栋信息
int insert_floor(floor a){
    const char *sql =
            "INSERT INTO floor (floor_name,tier_count,time) VALUES (?, ?, ?);";
    sqlite3_stmt *stmt;
    int ls = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
    const char *floor_name =a.floor_name;
    int tier_count = a.tier_count;
    const char *house_time=a.time;
    sqlite3_bind_text(stmt, 1, floor_name, -1, SQLITE_TRANSIENT);
    sqlite3_bind_int(stmt, 2, tier_count);
    sqlite3_bind_text(stmt, 3, house_time,-1,SQLITE_TRANSIENT);
// 执行语句
    ls = sqlite3_step(stmt);
    if (ls != SQLITE_DONE) {
        sqlite3_finalize(stmt);
        return -1;
    }
    sqlite3_finalize(stmt);
    return 0;
}
//查询楼栋信息
//查询住户信息
void select_floor(char * floor_name){
    const char * sql;
    if(strcmp(floor_name,"all")==0){
        sql = "select floor_id as '楼栋编号', floor_name as '楼栋名称',tier_count as '层数',time as '建成日期' from floor;";
    }else{
        sql = "select floor_id as '楼栋编号', floor_name as '楼栋名称',tier_count as '层数',time as '建成日期' from floor where floor_name=?;";
    }
    sqlite3_stmt * stmt;
    sqlite3_prepare_v2(db,sql,-1,&stmt,NULL);
    sqlite3_bind_text(stmt,1,floor_name,-1,SQLITE_TRANSIENT);
    //查列数
    int floor_head_count = sqlite3_column_count(stmt);
    //据算行数
    int a=0;
    while (sqlite3_step(stmt)==SQLITE_ROW){
        a++;
    }
    if(a==0){
        printf("没有该楼！\n");
        sqlite3_finalize(stmt);
        return;
    }
    //定义存放列最大长度长度数组,初始化为0
    int len[floor_head_count];
    for (int i = 0; i <floor_head_count ; ++i) {
        len[i]=0;
    }
    //用来存放int类型值 用于拼接
    char AAA[50];
    int z=0;
    while (sqlite3_step(stmt)==SQLITE_ROW){
        int floor_id = sqlite3_column_int(stmt,0);
        sprintf(AAA,"%d",floor_id);
        if(len[z]< strlen(AAA)){
            len[z]= strlen(AAA);
        }
        z++;
    }
    z=0;
    while (sqlite3_step(stmt)==SQLITE_ROW){
        const char * floor_name = sqlite3_column_text(stmt,1);
        if(len[z]< strlen(floor_name)){
            len[z]= strlen(floor_name);
        }
        z++;
    }
    z=0;
    while (sqlite3_step(stmt)==SQLITE_ROW){
        int tier_count= sqlite3_column_int(stmt,2);
        sprintf(AAA,"%d",tier_count);
        if(len[z]< strlen(AAA)){
            len[z]= strlen(AAA);
        }
        z++;
    }
    while (sqlite3_step(stmt)==SQLITE_ROW){
        const char * time = sqlite3_column_text(stmt,3);
        if(len[z]< strlen(time)){
            len[z]= strlen(time);
        }
        z++;
    }
    z=0;
    for (int i = 0; i < floor_head_count; i++) {
        //len1存放列名长度
        int len1=strlen(sqlite3_column_name(stmt, i));
        len1=len1;
        if(len[i]<len1){
            len[i]=len1+5;

        }
//        printf("----%d----",len[i]);
        printf("%-*s",len[i],sqlite3_column_name(stmt, i));
    }
    printf("\n");
    for (int i = 0; i <floor_head_count ; ++i) {
        len[i]=len[i]-5;
    }
    while (sqlite3_step(stmt)==SQLITE_ROW){
        int floor_id = sqlite3_column_int(stmt,0);
        const char * floor_name = sqlite3_column_text(stmt,1);
        int tier_count= sqlite3_column_int(stmt,2);
        const char * time = sqlite3_column_text(stmt,3);
        printf("%-*d",len[0],floor_id);
        printf("%-*s",len[1]+5/3,floor_name);
        printf("%-*d",len[2]+5/3,tier_count);
        printf("%-*s",len[3],time);
        printf("\n");
    }
    //释放语句句柄
    sqlite3_finalize(stmt);
}
//根据楼栋名称删除楼
int delete_floor(char * floor_name){
    sqlite3_stmt * stmt;
    char  sql1[250];
    sprintf(sql1,"select floor_name from floor where floor_name='%s';",floor_name);
    sqlite3_prepare_v2(db,sql1,-1,&stmt,NULL);
    int a=0;
    while (sqlite3_step(stmt)==SQLITE_ROW){
        a++;
    }
    if(a==0){
        printf("没有该楼栋，请重新输入:\n");
        sqlite3_finalize(stmt);
        return -1;
    }
    char sql[250];
    sprintf(sql,"delete from floor where floor_name= '%s';",floor_name);
    sqlite3_exec(db,sql,NULL,NULL,NULL);
    printf("删除成功!\n");
    sqlite3_finalize(stmt);
    return 0;
}
//用于判断楼栋是否存在
int judge_floor(char *floor_name){
    int a=0;
    sqlite3_stmt * stmt;
    char sql1[250];
    sprintf(sql1,"select floor_name from floor where floor_name='%s';",floor_name);
    sqlite3_prepare_v2(db,sql1,-1,&stmt,NULL);
    while (sqlite3_step(stmt)==SQLITE_ROW){
        a++;
    }
    sqlite3_finalize(stmt);
    if(a==0){
        printf("没有该楼栋，请重新输入:\n");
        return -1;
    }
    return 0;
}
//用于更改信息的二次导航
void update_floor(char *floor_name){
    int a;//用于导航选择
    int b=1;//循环条件
    int c=0;//避免重复提醒
    char d[50];//存放新改的内容
    int e;//存放避免重复楼栋
    int f;//用于存放更改int类型数据
    printf("----------------请选择要更改:%s的什么内容----------------\n",floor_name);
    printf("1.更改楼栋名称 2.更改楼层数 3.更改创建时间  4.退出\n");
    a= hcq(a);
    while(b){
        if(c!=0){
            printf("----------------请选择要更改:%s的什么内容----------------\n",floor_name);
            printf("1.更改楼栋名称 2.更改楼层数 3.更改创建时间  4.退出\n");
            a= hcq(a);
        }
        c++;
        switch (a) {
            case 1:
                printf("请输入新楼栋名！\n");
                scanf("%s",d);
                e=update_floor2(floor_name,d,"floor_name");
                if(e==-1){
                    printf("该楼栋已存在，更改失败\n");
                }else{
                    printf("更改成功！\n");
                }
                break;
            case 2:
                printf("请输入新楼层数！\n");
                scanf("%d",&f);
                e=update_floor3(floor_name,f,"tier_count");
                if(e!=-1){
                    printf("更改成功！\n");
                }
                break;
            case 3:
                printf("请输入建立新日期！\n");
                scanf("%s",d);
                e=update_floor2(floor_name,d,"time");
                if(e!=-1){
                    printf("更改成功！\n");
                }
                break;
            case 4:
                b=0;
                break;
            default:
                printf("输入错误请重新输入:\n");
                break;
        }
    }
}
int update_floor2(char *floor_name,char * new,char * head){
    char sql[250];
    sprintf(sql,"update floor set %s='%s' where floor_name='%s';",head,new,floor_name);
    int ls=sqlite3_exec(db,sql,NULL,NULL,NULL);
    if (ls!=SQLITE_OK){
        return -1;
    }
    return 0;
}
int update_floor3(char *floor_name,int new,char * head){
    char sql[250];
    sprintf(sql,"update floor set %s=%d where floor_name='%s';",head,new,floor_name);
    int ls=sqlite3_exec(db,sql,NULL,NULL,NULL);
    if (ls!=SQLITE_OK){
        return -1;
    }
    return 0;
}